package com.lfp.config.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 备份交换机 配置类
 *
 *  交换机、队列关系图参考：./images/备份交换机.png
 *
 *  注意：
 *      1、主交换机声明时，指定备份交换机
 *      2、fanout模式的交换机，队列绑定时，不需要key
 *
 * @Author lfp
 * @Date 2022/7/19
 * @Version 1.0
 */
@Configuration
public class BackupExchangeQueueConfig {

    //主交换机名称
    public static final String MASTER_EXCHANGE_NAME = "master.exchange";
    //主队列名称
    public static final String MASTER_QUEUE_NAME = "master.queue";
    //主队列路由键
    public static final String MASTER_QUEUE_ROUTING_KEY = "key1";

    //备份交换机名称
    public static final String BACKUP_EXCHANGE_NAME = "backup.exchange";
    //备份队列名
    public static final String BACKUP_QUEUE_NAME = "backup.queue";
    //预警队列名
    public static final String WARNING_QUEUE_NAME = "warning.queue";


    //声明主交换机
    @Bean
    public DirectExchange masterExchange(){
        Map<String, Object> arguments = new HashMap<>();
        //声明主交换机的备份交换机
        arguments.put("alternate-exchange",BACKUP_EXCHANGE_NAME);

        return ExchangeBuilder
                .directExchange(MASTER_EXCHANGE_NAME)
                .durable(true)
                .withArguments(arguments)//添加构建参数
                .build();
    }

    //声明备份 fanout交换机
    @Bean
    public FanoutExchange backupExchange(){
        return  ExchangeBuilder
                .fanoutExchange(BACKUP_EXCHANGE_NAME)
                .durable(true)
                .build();
    }

    //声明主队列
    @Bean
    public Queue masterQueue(){
        return QueueBuilder
                .durable(MASTER_QUEUE_NAME)
                .build();
    }

    //声明备份队列
    @Bean
    public Queue backupQueue(){
        return QueueBuilder
                .durable(BACKUP_QUEUE_NAME)
                .build();
    }
    //声明预警队列
    @Bean
    public Queue warningQueue(){
        return QueueBuilder
                .durable(WARNING_QUEUE_NAME)
                .build();
    }

    //绑定 将主队列绑定到主交换机
    @Bean
    public Binding bindMasterQueueToConfirmExchange(
            @Qualifier("masterQueue") Queue masterQueue,
            @Qualifier("masterExchange") DirectExchange masterExchange){

        return BindingBuilder
                .bind(masterQueue)
                .to(masterExchange)
                .with(MASTER_QUEUE_ROUTING_KEY);
    }

    //绑定 将备份队列绑定到备份交换机
    @Bean
    public Binding bindBackupQueueToBackupExchange(
            @Qualifier("backupQueue") Queue backupQueue,
            @Qualifier("backupExchange") FanoutExchange backupExchange){

        return BindingBuilder
                .bind(backupQueue)
                .to(backupExchange);
    }
    //绑定 将预警队列绑定到备份交换机
    @Bean
    public Binding bindWarningQueueToBackupExchange(
            @Qualifier("warningQueue") Queue warningQueue,
            @Qualifier("backupExchange") FanoutExchange backupExchange){

        return BindingBuilder
                .bind(warningQueue)
                .to(backupExchange);
    }
}
